import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/model/group_details_entity.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/commom/address_book/address_list_logic.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/rong_base_logic.dart';
import 'package:flutter_ncsjqz_getx/util/get_user_util.dart';
import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:get/get.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';

class ChatsSettingLogic extends RongBaseLogic {
  // 免打扰  false关闭  true开启
  bool disturb = false;
  TextEditingController textEditingControllerGroupName = TextEditingController();
  TextEditingController textEditingControllerUserName = TextEditingController();
  GroupDetailsData groupDetailsData = GroupDetailsData();

  getData(groupId) async{
    await getGroup(groupId).then((value) {
      groupDetailsData = value;
      disturb = groupDetailsData.isDisturb ?? false;
      textEditingControllerGroupName.text = groupDetailsData.groupName ?? '';
      update();
    });
  }

  setDisturb(){
    disturb = !disturb;
    update();
    setGroup();
  }

  /// 设置群名称、我在群内名称和免打扰
  setGroup() async{
    Map<String,dynamic> map={
      "groupId":  groupDetailsData.groupId,
      "groupName": textEditingControllerGroupName.text,
      "isDisturb": disturb,
      "isReview": groupDetailsData.isReview,
    };
    BaseEntity baseEntity = await DioService<BaseEntity>().updateGroup_v2(map);
    if(baseEntity.code == 0){
      setConversationNotificationStatus(3, groupDetailsData.groupId, disturb);
      groupDetailsData.groupName = textEditingControllerGroupName.text;
      toast("更新成功");
      update();
    }
  }

  /// 退出群聊
  outUser(ryUserId,nikeName) async{
    var map = {
      'groupName':groupDetailsData.groupName,
      'groupId':groupDetailsData.groupId,
      'ryUserId':ryUserId,
    };
    BaseEntity baseEntity = await DioService<BaseEntity>().quitGroup(map);
    if(baseEntity.code == 0){
      String name = "";
      groupDetailsData.users!.forEach((element) {
        if(element.ryUserId == getUserUtil().ryUserId){
          name = element.remarkName ?? '';
        }
      });
      //踢人的消息
      GroupNotificationMessage groupNotificationMessage =GroupNotificationMessage();
      groupNotificationMessage.operation=GroupNotificationMessage.GROUP_OPERATION_KICKED;//修改群名称
      groupNotificationMessage.message="$name将$nikeName踢出群聊";
      groupNotificationMessage.data= ryUserId;//修改后的信息
      groupNotificationMessage.operatorUserId=getUserUtil().ryUserId;
      groupNotificationMessage.sendUserInfo=UserInfo()..userId=getUserUtil().ryUserId ..name=getUserUtil().nikeName;
      Message? msg =await RongIMClient.sendMessage(RCConversationType.Group, groupDetailsData.groupId!, groupNotificationMessage,disableNotification: true);
      RongIMClient.onMessageSend=(int? messageId, int? status, int? code)async{
        if(code==0){
          Message? content=await RongIMClient.getMessage(msg!.messageId!);
          // bus.send(Constants.rcMessageArrived, {'message': content, 'left': 0,});//聊天
          // 发送eventBus事件信息
          Constant.eventBus.fire(EventFn("rong",{'message': content, 'left': 0,}));
        }
      };
      toast("踢人成功");
    }
  }

  /// 加入群聊
  addGroup(List<ContactInfo?> user) async{
    List users = []; // 邀请的人
    user.forEach((element) {
      users.add({
        "picUrl":element!.img,
        "ryUserId":element.ryUserId,
        "userId":element.friendId,
        "remarkName":element.name,
      });
    });
    var map = {
      // 群信息
      'groupName': groupDetailsData.groupName,
      'groupId': groupDetailsData.groupId,
      "users": users,
      // 邀请
      "joinType":1
    };
    BaseEntity baseBeanEntity =await DioService<BaseEntity>().joinGroup(map);
    if(baseBeanEntity.code == 0){
      toast("邀请好友加入群聊成功");
      getData(groupDetailsData.groupId,);
    }
  }

  /// 解散群
  void outGroup() async{
    var map = {
      "groupId": groupDetailsData.groupId,
      "ryUserId": getUserUtil().ryUserId,
    };
    BaseEntity baseEntity = await DioService<BaseEntity>().quitGroup(map);
    if(baseEntity.code == 0){
      deleteMessages(groupDetailsData.groupId,(v){
        Get.back();
        Get.back();
        Get.back();
      },rcConversationType: 3);
    }
  }
}
